Skip to content

[ENG-12545] fix(gateway): let a terminating gateway finish in-flight execs before it exits - #30

Merged
mark-kindo merged 2 commits into
mainfrom
cameron/eng-12545-gateway-drain
Nov 5, 2026
Merged

mark-kindo merged 2 commits into
mainfrom
cameron/eng-12545-gateway-drain

Conversation

@link108

@link108 link108 commented Nov 4, 2026 •

Copy link
Copy Markdown

Summary

A terminating gateway now finishes the execs it is relaying before it exits, instead of dropping them partway through. A second commit stops supervisors from redialing a departed gateway once its session has ended, which matters once the supervisor's hold on a departed gateway is sized to that drain. Kindo ticket: ENG-12545.

Related Issue

No issue required: fork-only fix, tracked in Kindo's Linear as ENG-12545.

Why

An exec is relayed over the supervisor session held by one replica, so it cannot fail over. On SIGTERM the gateway stopped its listeners, withdrew its liveness records and returned. The connection tasks still relaying execs are detached, so the runtime dropped them, and every in-flight exec failed after the command might already have had side effects. Every gateway rollout and node drain did this.

Changes

  • Refuse new relays while draining. SupervisorSessionRegistry::begin_drain makes session_or_unavailable answer with the existing no-session refusal, carrying the serving-replicas hint for the peers. Callers already route that refusal elsewhere, and it is still reached before dispatch, so it still proves nothing ran.

  • Stop renewing liveness. Keeper loops exit once draining, so a renew failure can't re-announce a record the shutdown just withdrew.

  • Count in-flight execs (exec_drain.rs). Both exec handlers take a guard before asking the registry for a relay. Shutdown starts draining the registry first and waits on the guards second, so every exec is either refused or waited for; no interleaving admits one without counting it.

  • Wait, bounded. After withdrawing liveness, shutdown waits for the count to reach 0, up to OPENSHELL_SHUTDOWN_DRAIN_SECONDS / --shutdown-drain-seconds. The default of 0 keeps the old behaviour. It exits as soon as the last exec finishes, so the value is a ceiling, not a delay.

  • Supervisor: hold a departed gateway only while its session lives (second commit). A departed subset member is held for OPENSHELL_FLEET_DRAIN_SECS so the execs riding its session can finish. Once that hold is sized to the gateway drain (hours), the old session loop would keep redialing an exited replica on a 30 s backoff for the whole window: about 360 failed dials per sandbox per exited gateway. Now the fleet loop marks the member departed, its session loop stops instead of reconnecting once the session fails, and the reaper drops the entry. A member that returns after its session ended is redialed rather than left with a dead entry.

Deployment note: the pod's terminationGracePeriodSeconds must exceed preStop plus the drain. Supervisors should hold a departed replica's session (OPENSHELL_FLEET_DRAIN_SECS) for at least as long, or they drop it after the default 120 s and kill the exec anyway. Kindo's chart sets both.

Testing

  • cargo test: openshell-server 1621 passed, openshell-core 495, plus the other suites. 5 new tests:
    • exec_drain: idle at once; returns when the last exec finishes; gives up at the deadline.
    • supervisor_session: a draining replica refuses new relays, names its peers and dispatches nothing; a draining replica does not announce liveness.
  • Supervisor: 171 passed, plus 2 new tests in gateway_fleet: a departed member is dropped once its session ends; a returning member whose session ended is redialed. Both fail without the fix: they time out because the loop keeps redialing.
  • cargo fmt --check clean. Clippy reports nothing in the changed lines.
  • Live on a local k3d cluster with 3 gateway replicas, driving execs through Kindo's sandbox-api. In each run a long exec starts, the gateway relaying it is deleted, and a new exec is sent every 2 s:
Run Long exec New execs Old pod
Control (stock gateway), pod deleted failed ~5 s after the delete 28/28 ok exited at once
This branch, pod deleted finished (90 s, exit 0) 60/60 ok exited as the exec ended
This branch, rollout restart finished (120 s, exit 0) all ok during rollout and drain both old pods exited as the exec ended
This branch, drain set to 20 s failed at 20 s, as expected 30/30 ok logged the deadline warning, exited

Repeated with the supervisor built from the second commit (a fresh sandbox): the exec survived the drain and all new execs succeeded. The stop-redialing behaviour isn't visible live, because the supervisor logs only warnings to stderr; the unit tests cover it.

The draining replica refused 4 execs in the first ~7 s; each was rerouted and succeeded. Traffic then left it once its records were gone.

Known edge: a relay that is pending at SIGTERM and needs a new TCP connection for its reverse CONNECT can still fail, because the listeners are already stopped. That window is milliseconds.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable): n/a

🤖 Generated with Claude Code

… it exits

An exec is relayed over the supervisor session the replica holds, so it
cannot fail over. On SIGTERM the gateway stopped accepting connections,
withdrew its liveness records and returned, and the runtime dropped the
detached connection tasks still relaying execs, failing each one partway
through.

Shutdown now drains. The session registry stops opening relays and
answers a new exec with the same refusal a replica without the session
gives, naming the peers that will outlast it, so callers route the work
elsewhere and the refusal still proves nothing ran. Liveness keepers stop
renewing, so the one-pass withdrawal at shutdown is not undone. Each exec
handler takes a guard before it asks for a relay, and shutdown waits for
the guards to drop, up to OPENSHELL_SHUTDOWN_DRAIN_SECONDS (default 0,
which keeps the old behaviour).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cameron motevasselani <cameron@kindo.ai>
@linear-code

linear-code Bot commented Nov 4, 2026

Copy link
Copy Markdown

ENG-12545

A departed subset member is held for the drain window so the execs riding
its session can finish. With the window sized to a gateway's shutdown
drain, which can be hours, the session loop kept redialing a replica that
had exited for that whole time, on a 30-second backoff.

The hold now lasts only while the session lives. The fleet loop marks a
departed member's session, its loop stops instead of reconnecting once
that session fails, and the reaper drops the entry. A member that returns
after its session ended is redialed rather than left with a dead entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cameron motevasselani <cameron@kindo.ai>
@mark-kindo
mark-kindo merged commit 768b330 into main Nov 5, 2026
12 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants